/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using DataModelException = System.Qizx.Api.DataModelException;
using EvaluationException = System.Qizx.Api.EvaluationException;
using QName = System.Qizx.Api.QName;
using BasicNode = System.Qizx.Xdm.BasicNode;
using CorePushBuilder = System.Qizx.Xdm.CorePushBuilder;
using XMLPushStreamBase = System.Qizx.Xdm.XMLPushStreamBase;
using BasicStaticContext = System.Qizx.XQuery.BasicStaticContext;
using EvalContext = System.Qizx.XQuery.EvalContext;
using ExprDisplay = System.Qizx.XQuery.ExprDisplay;
using Focus = System.Qizx.XQuery.Focus;
using ModuleContext = System.Qizx.XQuery.ModuleContext;
using XQType = System.Qizx.XQuery.XQType;
namespace System.Qizx.XQuery.Op
{
	
	/// <summary> class ElementConstructor:</summary>
	public class ElementConstructor:NamedConstructor
	{
		public override int Flags
		{
			get
			{
				// single Node: necessarily at same_depth!
				return DOCUMENT_ORDER + SAME_DEPTH + flags;
			}
			
		}
		public System.Collections.ArrayList attributes = new System.Collections.ArrayList();
		
		public ElementConstructor(Expression name):base(name)
		{
		}
		
		public virtual void  addAttribute(AttributeConstructor attribute)
		{
			attributes.Add(attribute);
		}
		
		public override void  putCharacters(char[] ch, int start, int length)
		{
			AddTextItem(new System.String(ch, start, length));
		}
		
		public virtual AttributeConstructor getAttribute(int rank)
		{
			return rank < 0 || rank >= attributes.Count?null:(AttributeConstructor) attributes[rank];
		}
		
		public override Expression Child(int rank)
		{
			if (rank == 0)
				return Name;
			rank -= 1;
			if (rank < attributes.Count)
				return getAttribute(rank);
			rank -= attributes.Count;
			return (rank < contents.Length)?contents[rank]:null;
		}
		
		public override void  Dump(ExprDisplay d)
		{
			d.header(this);
			d.child(Name);
			for (int i = 0, size = attributes.Count; i < size; i++)
				d.child(getAttribute(i));
			d.children(contents);
		}
		
		public override Expression StaticCheck(ModuleContext context, int flags)
		{
			base.StaticCheck(context, flags);
			for (int e = 0, E = attributes.Count; e < E; e++)
			{
				Expression attr = context.staticCheck(getAttribute(e), 0);
				attributes[e] = attr;
				if (attr is PathExpr)
					((PathExpr) attr).tryToTrim();
			}
			for (int c = 0; c < contents.Length; c++)
				if (contents[c] is PathExpr)
					((PathExpr) contents[c]).tryToTrim();
			
			type = XQType.ELEMENT;
			return this;
		}
		
		public override BasicNode EvalAsNode(Focus focus, EvalContext context)
		{
			BasicStaticContext staticContext = context.StaticContext;
			// we arrive here if building nodes is really wanted
			CorePushBuilder builder = new CorePushBuilder(staticContext.BaseURI, staticContext.InScopeNS);
			// for dynamic qname evaluation: make inscope ns visible
			context.InScopeNS = builder.NamespaceContext;
			EvalAsEvents(builder, focus, context);
			return builder.harvest();
		}
		
		public override void  EvalAsEvents(XMLPushStreamBase output, Focus focus, EvalContext context)
		{
			context.at(this);
			QName qname = EvalName(output, focus, context);
			
			try
			{
				output.putElementStart(qname);
				
				for (int a = 0; a < attributes.Count; a++)
				{
					try
					{
						getAttribute(a).EvalAsEvents(output, focus, context);
					}
					catch (EvaluationException e)
					{
						if ((Flags & DIRECT) != 0)
							e.substituteCode("XQDY0025", "XQST0040");
						throw e;
					}
				}
				for (int c = 0; c < contents.Length; c++)
				{
					contents[c].EvalAsEvents(output, focus, context);
					output.noSpace();
				}
				output.putElementEnd(qname);
			}
			catch (DataModelException e)
			{
				dmError(context, e);
			}
		}
	}
}